﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class PortBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IPortDA portDA = DataAccess.CreatePortDA();
        private PubParasInfo pubParasInfo;

        public PortBLL()
        {
            this.pubParasInfo = null;
        }

        public PortBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public PortBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> IDS)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = string.Empty;
            try
            {
                List<PortInfo> PortInfos = new List<PortInfo>();
                PortInfo portInfo = null;
                foreach (string id in IDS)
                {
                    portInfo = portDA.GetPortByID(dataBase, tran, id, this.dataBaseID[0]);
                    PortInfos.Add(portInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (portDA.DeletePort(dataBase, tran, IDS, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<PortInfo>(dataBase, tran, PortInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "PORT", this.dataBaseID[0]);
                        temp = temp + IDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return portDA.GetAllPort(this.dataBaseID[0]);
        }

        public object GetInfoByID(string id)
        {
            return portDA.GetPortByID(id, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            PortQueryEntity portQueryEntity = obj as PortQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(portQueryEntity.PortID))
            {
                sqlWhere.Add(" PORT_ID " + portQueryEntity.PortID);
            }
            if (!string.IsNullOrEmpty(portQueryEntity.PortName))
            {
                sqlWhere.Add(" PORT_NAME " + portQueryEntity.PortName);
            }
            if (!string.IsNullOrEmpty(portQueryEntity.Remark))
            {
                sqlWhere.Add(" REMARK " + portQueryEntity.Remark);
            }
            if (!string.IsNullOrEmpty(portQueryEntity.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE ='" + portQueryEntity.IsActive + "'");
            }
            return portDA.GetPortByQueryList(sqlWhere, portQueryEntity, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            PortInfo portInfo = obj as PortInfo;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strPortID = string.Empty;
            try
            {
                if (portDA.CheckPortIDUniqueInsert(dataBase, tran, portInfo.PortID, whLoginID))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F9110_003", this.pubParasInfo.UserLanguage, new List<string> { portInfo.PortID });
                }
                if (string.IsNullOrEmpty(portInfo.PortID))
                {
                    portInfo.PortID = commonMethod.GenNextNumStr(dataBase, tran, "PORT", whLoginID);
                }
                portInfo.UpdatedBy = portInfo.CreatedBy;
                portInfo.CreatedDate = DateTime.Now;
                portInfo.UpdatedDate = DateTime.Now;
                portInfo.WhID = whLoginID;
                if (portDA.InsertPort(dataBase, tran, portInfo, whLoginID) > 0)
                {
                    strPortID = portInfo.PortID;
                    new AuditDataBLL().InsertAuditData<PortInfo>(dataBase, tran, null, portInfo, AuditAction.Insert, this.pubParasInfo.UserID, "PORT", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strPortID;
        }

        public string Update(object obj)
        {
            PortInfo portInfo = obj as PortInfo;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strPortID = string.Empty;
            try
            {
                PortInfo oldportInfo = portDA.GetPortByID(dataBase, tran, portInfo.PortID, this.dataBaseID[0]);
                if (portDA.UpdatePort(dataBase, tran, portInfo, whLoginID) > 0)
                {
                    strPortID = portInfo.PortID;
                    new AuditDataBLL().InsertAuditData<PortInfo>(dataBase, tran, oldportInfo, portInfo, AuditAction.Update, this.pubParasInfo.UserID, "PORT", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strPortID;
        }
    }
}

